package com.ghca.myinfo.spider.processor;

import com.ghca.myinfo.common.ProjectType;
import com.ghca.myinfo.common.Source;
import com.ghca.myinfo.common.TenderType;
import com.ghca.myinfo.market.entities.TenderNotice;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.handler.SubPageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.HtmlNode;
import us.codecraft.webmagic.selector.Selectable;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/1/22.
 */
@Component
public class SCZBBXDetailProcessor implements SubPageProcessor{

    private static final String URL = "https://www.sczbbx.com/Project/ShowProject.aspx\\?pid=.*";

    private static final String[] DATE_PATTERNS = {"yyyy年MM月dd日","yyyy年MM月dd日hh时", "yyyy年MM月dd日hh时mm分"};

    public SCZBBXDetailProcessor() {
    }

    @Override
    public boolean match(Request page) {
        return Pattern.compile(URL).matcher(page.getUrl()).matches();
    }

    @Override
    public MatchOther processPage(Page page) {
        Html html = page.getHtml();
        String name = html.xpath("//div[@id='details']/h3/span/text()").toString();
        String startDate = html.xpath("//span[@id='lb_start']/text()").toString();
        String endDate = html.xpath("//span[@id='lb_end']/text()").toString();
        String region = html.xpath("//span[@id='lb_region']/text()").toString();
        String tenderee = html.xpath("//div[@id='details']//table[@class='details_tab'][1]//tr[1]/td/span/text()").toString();
        String projectTypeString = html.xpath("//div[@id='details']//strong//span").nodes().get(4).toString();
        HtmlNode detailsNode = (HtmlNode) html.xpath("//div[@id='details']//div[1]//table[@class='details_tab gridview']//tr");
        int projectType = 0;
        for(ProjectType pt : ProjectType.values()) {
            if(projectTypeString.contains(pt.getName())) {
                projectType = projectType|pt.getKey();
            }
        }
        List<Selectable> nodes = detailsNode.nodes();
        List<TenderNotice> tenderNotices = new ArrayList<>(nodes.size());
        for (Selectable node : nodes) {
            TenderNotice tenderNotice = new TenderNotice();
            tenderNotice.setTenderee(tenderee);
            try {
                tenderNotice.setStartDate(DateUtils.parseDate(startDate, DATE_PATTERNS));
                tenderNotice.setEndDate(DateUtils.parseDate(endDate, DATE_PATTERNS));
            } catch (ParseException exception) {
                exception.printStackTrace();
            }
            tenderNotice.setRegion(StringUtils.split(region, '-')[0]);
            String type = node.xpath("//td[1]/text()").toString();
            for (TenderType t : TenderType.values()) {
                if (type.startsWith(t.getName())) {
                    tenderNotice.setType(t.getKey());
                    if (nodes.size() > 1) {
                        String segment = "标段" + type.substring(type.indexOf(t.getName()) + t.getName().length());
                        tenderNotice.setName(name+segment);
                    }
                    else {
                        tenderNotice.setName(name);
                    }
                    break;
                }
            }
            String alias = node.xpath("//td[2]/text()").toString();
            tenderNotice.setAlias(alias);
            tenderNotice.setProjectType(projectType);
            tenderNotice.setUrl(page.getRequest().getUrl());
            tenderNotice.setSource(Source.SCZBBX.ordinal());
            tenderNotices.add(tenderNotice);
        }
        page.putField("tenderNotices", tenderNotices);
        return MatchOther.NO;
    }



}
